package com.example.springboot.demo.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.springboot.demo.entity.*;
import com.example.springboot.demo.service.ExportService;
import com.google.gson.Gson;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.*;
import java.util.*;

@RestController
@RequestMapping("export")
public class ExportController {

    @Resource
    private ExportService exportVentor;

    @GetMapping("vendor")
    public void listRelation() throws IOException {
        try (BufferedReader br = new BufferedReader(new FileReader("D:/text/vendor.json.txt"))) {
            String jsonStr = br.readLine();
            JSONArray jsonArray = JSONUtil.parseArray(jsonStr);
//            JSONArray jsonArray = new JSONArray("[{\"id\":248,\"name_en\":\"AIS\",\"introduction\":1,\"definedStr\":\"系统预定义\",\"contact\":\"\",\"phone_number\":\"\",\"desc\":\"\"},{\"id\":247,\"name_en\":\"DZEC\",\"introduction\":1,\"definedStr\":\"系统预定义\",\"contact\":\"\",\"phone_number\":\"\",\"desc\":\"\"}]");
            Gson gson = new Gson();
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Vendor ventor = gson.fromJson(jsonObject.toString(), Vendor.class);
//                ventor.setId((long) Math.abs(UUID.randomUUID().hashCode()));
                ventor.setCreate_user(181009439703171072L);
                if (exportVentor.exportVentor(ventor) > 0) {
                    System.out.println(ventor.getId() + "导入成功");
                }
            }
        } finally {
            System.out.println("读取完毕");
        }
    }

    @GetMapping("series")
    public void series() throws IOException {
        try (BufferedReader br = new BufferedReader(new FileReader("D:/text/series.json.txt"))) {
            String jsonStr = br.readLine();
            JSONArray jsonArray = JSONUtil.parseArray(jsonStr);
            br.close();
            Gson gson = new Gson();
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Series series = gson.fromJson(jsonObject.toString(), Series.class);
                series.setCreate_user(181009439703171072L);
                if (exportVentor.exportSeries(series) > 0) {
                    System.out.println(series.getId() + "导入成功");
                }
            }
        } finally {
            System.out.println("读取完毕");
        }
    }

    @GetMapping("model")
    public void model() throws IOException {
        try (BufferedReader br = new BufferedReader(new FileReader("D:/text/series.json.txt"))) {
            String jsonStr = br.readLine();
            JSONArray jsonArray = JSONUtil.parseArray(jsonStr);
            br.close();
            Gson gson = new Gson();
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Series series = gson.fromJson(jsonObject.toString(), Series.class);
                JSONArray objects = JSONUtil.parseArray(series.getRsDeviceTypeEntities());
                Gson gson1 = new Gson();
                for (int i1 = 0; i1 < objects.size(); i1++) {
                    JSONObject jsonObject1 = objects.getJSONObject(i1);
                    Model model = gson1.fromJson(jsonObject1.toString(), Model.class);
                    int modelId = exportVentor.selectById(model.getId());
                    if (modelId > 0){
                        int category = exportVentor.updateCategory(model);
                        if (category > 0){
                            System.out.println(modelId + " 导入成功！");
                        }
                    }
                }
            }
            System.err.println("导入完毕!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping("id")
    public void id() throws IOException {
        List<EnterpriseDvendor> enterpriseDvendorList = exportVentor.selectId();
        for (EnterpriseDvendor enterpriseDvendor : enterpriseDvendorList) {
            String randomString = generateRandomString(15);
            enterpriseDvendor.setId(Long.parseLong("177" + randomString));
//            System.out.println("insert into snmp_enterprise_id value" + "(" + enterpriseDvendor.getId() + "," +enterpriseDvendor.getEnterpriseId() + "," +enterpriseDvendor.getVendorId() + ");");
//            if (exportVentor.insertse(enterpriseDvendor)){
//                System.err.println(enterpriseDvendor.getId() + "导入成功！");
//            }
        }
    }

    private static String generateRandomString(int length) {
        String characters = "0123456789";
        StringBuilder stringBuilder = new StringBuilder(length);
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(characters.length());
            char randomChar = characters.charAt(randomIndex);
            stringBuilder.append(randomChar);
        }
        return stringBuilder.toString();
    }

    @GetMapping("es")
    public void es() throws IOException {
        Set<Long> ids = new HashSet<>();
        Set<Long> deviceModelAll = new HashSet<>();
        Map<Integer, Integer> map = new HashMap<>();
        map.put(11, 6);
        map.put(1751, 65);
        map.put(738, 67);
        map.put(2925, 90);
        map.put(36, 34);
        map.put(52, 34);
        map.put(297, 208);
        map.put(629, 142);
        map.put(789, 147);
        map.put(8072, 149);
        map.put(763, 19);
        map.put(1548, 19);
        map.put(99, 199);
        map.put(253, 208);
        map.put(2021, 9);
        map.put(6527, 7);
        map.put(25506, 7);
        map.put(171, 5);
        map.put(1588, 7);
        map.put(10734, 7);
        map.put(232, 7);
        map.put(1, 1);
        map.put(4, 60);
        map.put(10519, 246);
//        map.put(738, 36);
//        map.put(25506, 234);
//        map.put(25506, 1);
//        map.put(8072, 9);
//        map.put(25506, 238);
//        map.put(25506, 3);
//        map.put(11, 60);
//        map.put(25506, 245);
//        map.put(25506, 247);
//        map.put(25506, 248);
//        map.put(25506, 13);
        for (Map.Entry<Integer, Integer> integerIntegerEntry : map.entrySet()) {
            List<Long> deviceModelList = exportVentor.selectEs(integerIntegerEntry.getKey(), integerIntegerEntry.getValue());
            deviceModelAll.addAll(deviceModelList);
        }
        int k = 0;
        for (Long l : deviceModelAll) {
            try (BufferedReader br = new BufferedReader(new FileReader("D:/text/series.json.txt"))) {
//                PrintWriter writer = new PrintWriter(new FileWriter("D:/text/输出json重复数据.txt"), true);
                //            fileOutputStream = new FileOutputStream(filepath);
                FileWriter fw=new FileWriter("D:/text/jsonchongfu.txt",true);
                String jsonStr = br.readLine();
                JSONArray jsonArray = JSONUtil.parseArray(jsonStr);
                br.close();
                Gson gson = new Gson();
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    Series series = gson.fromJson(jsonObject.toString(), Series.class);
                    if (l.equals(series.getId())) {
                        k++;
                    }
                    if (k > 0) {
                        fw.write(jsonObject + ",");
                        System.out.println(l + " 读取完毕");
                    }
                }
                fw.close();
            }
        }
        System.err.println("输出结束");
    }
    @GetMapping("again")
    public void selectAgain() throws IOException {
        Set<Long> ids = new HashSet<>();
        Map<Integer, Integer> map = new HashMap<>();
//        map.put(11, 6);
//        map.put(1751, 65);
//        map.put(738, 67);
//        map.put(2925, 90);
//        map.put(36, 34);
//        map.put(52, 34);
//        map.put(297, 208);
//        map.put(629, 142);
//        map.put(789, 147);
//        map.put(8072, 149);
//        map.put(763, 19);
//        map.put(1548, 19);
//        map.put(99, 199);
//        map.put(253, 208);
//        map.put(2021, 9);
//        map.put(6527, 7);
//        map.put(25506, 7);
//        map.put(171, 5);
//        map.put(1588, 7);
//        map.put(10734, 7);
//        map.put(232, 7);
//        map.put(1, 1);
//        map.put(4, 60);
//        map.put(10519, 246);
//        map.put(738, 36);
//        map.put(25506, 234);
//        map.put(8072, 9);
//        map.put(11, 60);
        map.put(25506, 1);
//        map.put(25506, 238);
//        map.put(25506, 3);
//        map.put(25506, 245);
//        map.put(25506, 247);
//        map.put(25506, 248);
//        map.put(25506, 13);
        for (Map.Entry<Integer, Integer> integerIntegerEntry : map.entrySet()) {
            List<Long> deviceModelList = exportVentor.selectEs(integerIntegerEntry.getKey(), integerIntegerEntry.getValue());
            ids.addAll(deviceModelList);
        }
        for (Long id : ids) {
            System.err.println(id);
        }
    }




}

